package com.kotlin.tsw.usercenter.ui.activity

import android.os.Bundle
import android.view.View
import com.kotlin.base.utils.AppPrefsUtils
import com.kotlin.tsw.baselibrary.ext.enable
import com.kotlin.tsw.baselibrary.ext.onClick
import com.kotlin.tsw.baselibrary.ui.activity.BaseMVPActivity
import com.kotlin.tsw.usercenter.R
import com.kotlin.tsw.usercenter.data.protocol.UserInfo
import com.kotlin.tsw.usercenter.injection.component.DaggerUserComponent
import com.kotlin.tsw.usercenter.injection.module.UserModule
import com.kotlin.tsw.usercenter.presenter.LoginPersenter
import com.kotlin.tsw.usercenter.presenter.view.LoginView
import com.kotlin.tsw.usercenter.utils.UserPrefsUtils
import kotlinx.android.synthetic.main.activity_login.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

class LoginActivity : BaseMVPActivity<LoginPersenter>(), LoginView, View.OnClickListener {
    override fun injectComponent() {
        DaggerUserComponent.builder().activityComponent(activityComponent).userModule(UserModule()).build().inject(this)
        mPresenter.mView = this
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        initView()
    }

    private fun initView() {
        mLoginBtn.enable(mMobileEt, { isButtonEnable() })
        mLoginBtn.enable(mPwdEt, { isButtonEnable() })
        mLoginBtn.setOnClickListener(this)
        mForgetPwdTv.setOnClickListener(this)
        mHeaderBar.getRightTv().onClick { startActivity<RegisterActivity>() }
    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.mLoginBtn -> {
//                mPresenter.login(mMobileEt.text.toString().trim(), mPwdEt.text.toString().trim(), "")
                startActivity<UserInfoActivity>()
                finish()
            }
            R.id.mForgetPwdTv -> {
                startActivity<ForgetPwdActivity>()
            }
        }
    }

    fun isButtonEnable(): Boolean {
        return !mMobileEt.text.isNullOrEmpty() &&
                !mPwdEt.text.isNullOrEmpty()
    }

    override fun onLoginResult(result: UserInfo) {
        UserPrefsUtils().putUserInfo(result)
        toast("登录成功")
    }
}
